<template>
  <div id="updateDialog">
    <el-dialog class="box" title="编辑设备" :visible.sync="updateDialogShow" :before-close="handleClose">
      <el-form :model="this.equipData" ref="equipData" label-width="120px" class="demo-form-inline"  >
        <el-form-item label="设备编号：">
          <el-col :span="9">
            <el-form-item >
              <el-input class="formInput"  v-model="equipData.cid" :span="24"></el-input>
            </el-form-item>
          </el-col>
          <el-col class="line formRight1 userid" :span="5" style="text-align: right"  ><span class="rightx"></span> <span style="color: #F56C84">*</span> <span>设备名称：</span></el-col>
          <el-col :span="9">
            <el-form-item prop="name" >
              <el-input class="formInput"  v-model="equipData.name" :span="24"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="通讯类型：" prop="type" required>
          <el-col :span="9">
            <el-form-item prop="type" >
              <el-select v-model="equipData.type" placeholder="请选择" :span="24">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col class="line formRight1 userid" :span="5" style="text-align: right" prop="addr">
            <span style="color: #F56C84">*</span> <span>通讯地址：</span>
          </el-col>
          <el-col :span="9">
            <el-form-item prop="addr" >
              <el-input class="formInput"  v-model="equipData.addr" :span="24"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="通讯参数：" required >
          <el-col :span="9">
            <el-form-item prop="param" >
              <el-input class="formInput" v-model="equipData.param"   :span="24"></el-input>
            </el-form-item>
          </el-col>
          <el-col class="line formRight1 userid" :span="5" style="text-align: right"><span class="rightx"></span>分类：</el-col>
          <el-col :span="9">
            <el-form-item prop="catagory" >
              <el-input class="formInput" v-model="equipData.catagory"   :span="24"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="品牌：" >
          <el-col :span="9">
            <el-form-item prop="brand" >
              <el-input class="formInput" v-model="equipData.brand"   :span="24"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <span class="foorterLeft">注：<span style="color: red">*</span>为必填项</span>
        <el-button type="primary" @click="updateEquipData" >保存</el-button>
        <el-button type="info" @click="handleClose" >取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import axios from 'axios'
  export default {
    props: {
      updateEquip: {},
      pid: ''
    },
    components: {
    },
    data() {
      return {
        restart: '更新',
        equipData: {},
        // queryType 获取通讯类容
        queryType: {
          access_token: localStorage.getItem('access_token'),
          type: 'object_get_conntype'
        },
        submitEquip: {
          access_token: localStorage.getItem('access_token'),
          type: 'object_condition',
          pid: 'ZD201809000001',
          data: {
            condition: {
              gid: '',
              cid: '',
              name: '',
              brand: '',
              catagory: '',
              param: '',
              type: '',
              addr: '',
              attris_file: ''
            },
            page: {
              page_size: '10',
              page_number: '1'
            }
          }
        },
        // 修改
        updateData: {
          access_token: localStorage.getItem('access_token'),
          type: 'object_mod',
          pid: 'default',
          data: {
            pid: '',
            gid: '',
            cid: 'plc0001',
            name: 'SIEMENS PLC 312',
            brand: 'SIEMENS',
            catagory: 'PLC',
            param: 'COM1:9600:1:N:3000:100',
            type: '485_modbus_new',
            addr: '01',
            attris_file: 'plc_siemens_plc312_modbus'
          }
        },
        assetform: {
          pid: '',
          name: '',
          type: '',
          addr: '',
          param: '',
          catagory: '',
          brand: '',
          pointName: ''
        },
        options: [],
        updateDialogFalse: false,
        isIndeterminate: true
      }
    },
    created() {
      console.log(this.updateEquip)
      console.log(this.pid)
      this.updateDialogShow = true
    },
    mounted() {
      this.queryEquipType()
      this.queryEquipData()
    },
    methods: {
      // 获取通讯类型
      queryEquipType() {
        axios.post('/project', this.queryType, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          console.log(res)
          this.options = res.data.datas
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      queryEquipData() {
        this.submitEquip.pid = this.pid
        this.submitEquip.data.condition.cid = this.updateEquip.cid
        axios.post('/project', this.submitEquip, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.equipData = res.data.data.items[0]
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      // 刷新列表
      restar() {
        this.$emit('diaStatus', this.restart)
      },
      // 修改
      updateEquipData() {
        if (this.equipData.name === '') {
          this.$message({
            type: 'error',
            message: '设备名称不能为空'
          })
        } else if (this.equipData.addr === '') {
          this.$message({
            type: 'error',
            message: '通讯地址不能为空'
          })
        } else if (this.equipData.param === '') {
          this.$message({
            type: 'error',
            message: '通讯参数不能为空'
          })
        } else {
          this.updateData.pid = this.pid
          this.updateData.data.pid = this.pid
          this.updateData.data.cid = this.equipData.cid
          this.updateData.data.name = this.equipData.name
          this.updateData.data.brand = this.equipData.brand
          this.updateData.data.catagory = this.equipData.catagory
          this.updateData.data.param = this.equipData.param
          this.updateData.data.type = this.equipData.type
          this.updateData.data.addr = this.equipData.addr
          this.updateData.data.attris_file = this.equipData.attris_file
          axios.post('/project', this.updateData, {
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded'
            }
          }).then((res) => {
            console.log(res)
            if (res.data.errcode === '00000') {
              this.handleClose()
              this.restar()
              this.$message({
                type: 'success',
                message: '编辑成功'
              })
            }
          })
            .catch(function(error) {
              this.$message({
                type: 'success',
                message: error
              })
            })
        }
      },
      handleCheckAllChange(val) {
        this.isIndeterminate = false
      },
      handleCheckedCitiesChange(value) {
        const checkedCount = value.length
        this.checkAll = checkedCount === this.details.length
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.details.length
      },
      // 子组件传值
      handleClose() {
        const dialogShow = this.updateDialogFalse // 所需要传的值
        this.$emit('giveFalse', dialogShow) // 传值的参数
        console.log(dialogShow)
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /*****************第一层弹窗*****************/
  .testColor{
    padding: 0;
    color: #fff;
  }
  .foorterLeft{
    width: 100px;
    height: 20px;
    font-size:14px;
    font-family:MicrosoftYaHei;
    font-weight:400;
    color:rgba(105,114,126,1);
    float: left;
  }
  /*****************第一层弹窗*****************/
  /deep/ .box > .el-dialog{
    width: 750px;
    height: 300px;
    margin-top: 30vh !important;
    border-radius: 4px;
  }
  /*****************第一层弹窗头部*****************/
  /deep/ .box > .el-dialog > .el-dialog__header{
    width: 750px;
    height: 39px;
    line-height: 43px;
    padding: 0;
    text-indent: 20px;
    font-weight: bolder;
    border-top-left-radius: 4px;
    border-top-right-radius: 4px;
    background-color: #e3e6ee;
  }
  /deep/ .box > .el-dialog > .el-dialog__header > .el-dialog__headerbtn{
    top: 12px;
  }
  /deep/ .box > .el-dialog > .el-dialog__header > .el-dialog__headerbtn > i{
    font-size: 18px;
    font-weight: bolder;
  }
  /*****************第一层弹窗内容*****************/
  /deep/ .box > .el-dialog > .el-dialog__body{
    padding: 0 0 0 10px ;
    min-height: 50px;
    line-height: 30px;
    margin-top: 20px;
    margin-bottom: 20px;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .el-form-item{
    margin-bottom: 0;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .el-form-item > .el-form-item__label{
    color: #69727E;
    font-weight: normal;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .el-form-item > .el-form-item__content > .el-input > .el-input__inner{
    width: 610px;
    height: 30px;
  }
  /*****************第一层弹窗底部*****************/
  /deep/ .box > .el-dialog > .el-dialog__footer{
    border-top: 1px solid #e3e6ee;
  }
  /*****************底部按钮*****************/
  /deep/ .dialog-footer{
    width: 100%;
    /*padding-top: 8px;*/
  }
  /deep/ .dialog-footer > .el-button{
    width: 80px;
    height: 30px;
    padding: 0;
  }
  /deep/ .el-input__inner {
    -webkit-appearance: none;
    background-color: #fff;
    background-image: none;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: inherit;
    height: 30px;
    line-height: 30px;
    outline: 0;
    padding: 0 10px;
    -webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    width: 100%;
  }
  /deep/ .el-form-item__error {
    color: #f56c6c;
    font-size: 12px;
    line-height: 1;
    padding-top: 4px;
    position: absolute;
    top: 100%;
    left: 15px;
    margin-top: -31px;
  }
</style>
